/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servidor.Controlador;

import BaseDatos.FachadaBBDD;
import Servidor.Modelo.Users.User;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * This class creates a Thread for get the users of the database
 * @author isabel
 * @author Jorge Perez
 */
public class ListUsersTask extends Thread implements OcaTask {

    private Socket socket;
    private BufferedReader read;
    private PrintWriter write;

    public ListUsersTask(Socket socket, BufferedReader read, PrintWriter write) {

        this.socket = socket;
        this.read = read;
        this.write = write;
    }

    @Override
    public void executeTask() {
        this.start();
    }

    /*
     * lee del array a los usuarios y lo manda por el socket
     */
    public void run() {
        ArrayList<User> listUsers = FachadaBBDD.get_instance().select_list_users();
        Iterator<User> iter = listUsers.iterator();
        
        while(iter.hasNext()){
            User user = iter.next();
            String userString = null;
            userString = String.valueOf(user.getId_user() + "#");
            userString += user.getNick() + "#";
            userString += user.getEmail() + "#";
            userString += user.getType_user() + "#";
            write.println(userString);
        }
        
        write.println(ConnectionConstraints.LIST_FINISH);
        new TrashTask(socket, read, write);
    }
}
